package com.atguigu.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 切面类：切人点+通知
 */
@Aspect
public class LogAspects {

    // 抽取公共的切入点表达式
    //@Pointcut(value = "execution(public int com.atguigu.aop.MathCalculator.div(int,int))")
    //@Pointcut(value = "execution(int com.atguigu.aop.MathCalculator.div(int,int))")
    //@Pointcut(value = "execution(* com.atguigu.aop.MathCalculator.div(int,int))")
    //@Pointcut(value = "execution(* com.atguigu.aop.MathCalculator.*(int,int))")
    @Pointcut(value = "execution(* com.atguigu.aop.MathCalculator.*(..))")
    public void pointCut(){}

    // 通知/增强(Advice):要增强的代码，即在目标方法执行前后要被执行的代码
    // 织入(Weaving):将通知应用到切入点的过程
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {
        System.out.println("[@Before]前置通知执行,方法签名是:"+joinPoint.getSignature()+",方法的参数列表是："+ Arrays.asList(joinPoint.getArgs()));
    }

    @After("pointCut()")
    public void logEnd() {
        System.out.println("[@After]后置通知执行(无论方法是否出现异常都会执行)");
    }

    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(Object result) {
        System.out.println("[@AfterReturning]返回通知执行(正常返回),方法返回结果是："+result);
    }

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void logException(Exception exception){
        System.out.println("[@AfterThrowing]异常通知执行,异常信息："+exception);
    }
}
